package org.spring.boot.data.reids;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author zhaomingqiang
 */
@Configuration
@EnableConfigurationProperties(RedisClientProperties.class)
public class RedisClientAutoConfig {

    @Autowired
    private RedisClientProperties redisClientProperties;

    @Bean
    @ConditionalOnMissingBean
    public JedisConnectionFactory getJedisConnectionFactory() {
        redisClientProperties.checkConfig();
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(redisClientProperties.getHostName());
        if (redisClientProperties.getPort() > 0) {
            jedisConnectionFactory.setPort(redisClientProperties.getPort());
        }
        if (!StringUtils.isEmpty(redisClientProperties.getPassword())) {
            jedisConnectionFactory.setPassword(redisClientProperties.getPassword());
        }
        if (redisClientProperties.isUseSsl()) {
            jedisConnectionFactory.setUseSsl(redisClientProperties.isUseSsl());
        }
        if (redisClientProperties.getTimeout() > 0) {
            jedisConnectionFactory.setTimeout(redisClientProperties.getTimeout());
        }
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        if (redisClientProperties.getPoolMaxSize() > 0) {
            jedisPoolConfig.setMaxTotal(redisClientProperties.getPoolMaxSize());
            jedisPoolConfig.setMaxIdle(redisClientProperties.getPoolMaxSize());
        }
        if (redisClientProperties.getPoolMinIdle() > 0) {
            jedisPoolConfig.setMinIdle(redisClientProperties.getPoolMinIdle());
        }
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
        return jedisConnectionFactory;
    }

    @Bean(name = "redisTemplate")
    @ConditionalOnMissingBean
    public RedisTemplate getRedisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(getJedisConnectionFactory());
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        return redisTemplate;
    }

    @Bean
    @ConditionalOnMissingBean
    public StringRedisTemplate getStringRedisTemplate() {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(getJedisConnectionFactory());
        return stringRedisTemplate;
    }
}
